home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Graphics Samples / Getting Started with GX (WWDC)ƒ / Getting Started GX - main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-15  |  10.0 KB  |  338 lines  |  [TEXT/KAHL]

  1. /**
  2.   --
  3.  --        App:        Getting Started w/QD GX (WWDC)
  4.  --
  5.  -- 
  6.  --        Version:    1.0     4/93:    added all of the calls required to support the "Getting 
  7.  --                                    Started with QuickDraw™ GX" session at the WWDC '93     
  8.  --
  9.  --                            8/93:    updated file to work with the ß2 "GXified" interface files
  10.  --                            9/93:    now overrides gxPrintingEvent for update event handling.
  11.  --                                    added GXUpdateJob call to resume event handling. - dmh
  12.  --            
  13.  --        File:        Getting Started GX - main.c
  14.  --
  15.  --
  16.  --        Comments:    This file is a part of a shell that can be used to build QuickDraw GX  
  17.  --                    applications. It contains all of the required calls to: testing for 
  18.  --                    QuickDraw GX, gxInitialize the QuickDraw GX world, and handling events.
  19.  --        
  20.  --                 For this shell to work correctly, it expects the following functions to
  21.  --                    be defined:
  22.  --
  23.  --                    void DoInitialization(WindowPtr);
  24.  --                    void DoDraw(WindowPtr);
  25.  --                    void DoDispose(WindowPtr);
  26.  --                    void DoClick(WindowPtr);
  27.  --                    void DoIdle(WindowPtr);
  28.  --
  29.  --                    By the way, if the QuickDraw GX printing system is not installed, we will
  30.  --                    not enable the print menu items in th File menu. The app will still draw
  31.  --                    QuickDraw GX shapes to the window, but it will not print them.
  32.  --
  33.  --
  34.  --
  35.  --        Components:    Getting Started GX - main.c
  36.  --                    Getting Started GX - main.h
  37.  --                    Getting Started GX - shapes.c
  38.  --                    Getting Started GX - printing.c
  39.  --                    Getting Started GX - misc.c
  40.  --                    Getting Started QD GX.π.rsrc
  41.  --
  42.  --                    The file titled: "Getting Started GX - main.c" contains the code required to
  43.  --                    intialize and tear down the QuickDraw GX world, and the event loop.
  44.  --
  45.  --                    The file titled: "Getting Started GX - shapes.c" contains of the code used to 
  46.  --                    create and manipulate the shapes draw into the window.
  47.  --        
  48.  --                    The file titled: "Getting Started GX - printing.c" contains of the code used to 
  49.  --                    print the contents of  the window.
  50.  --        
  51.  --                    The file titled: "Getting Started GX - misc.c" contains of the code for the utilities used.
  52.  --        
  53.  --        
  54.  --        QuickDraw GX
  55.  --        Libraries
  56.  --        Used:        This application uses the following QuickDraw GX library code files:
  57.  --                    "gxColor library.c", "gxFont library.c", "graphics debug library.c",
  58.  --                    "layout library.c", "qd library.c", "gxShape library.c", 
  59.  --                    "gxTransferMode library.c", and "gxTransform library.c". 
  60.  --        
  61.  --        
  62.  --        Notes:        1) Print this file in landscape for the best results
  63.  --                    2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
  64.  --                    3) This code was adapted from the "Banana Jr." QuickDraw GX sample.
  65.  --
  66.  --
  67.  --        Author:        Pete "Luke" Alexander
  68.  --                    Developer Technical Support
  69.  --                    AppleLink: DEVSUPPORT
  70.  --
  71.  --        
  72.  --        ©1990 - 1993  Apple Computer, Inc. 
  73.  --        All rights reserved.
  74.  --
  75.  **/
  76.  
  77.  
  78. #include <Desk.h>
  79. #include <Events.h>
  80. #include <Fonts.h>
  81. #include <Windows.h>
  82. #include <Memory.h>
  83. #include <ToolUtils.h>
  84. #include <Menus.h>
  85. #include <Quickdraw.h>
  86.  
  87. #include "PrintingManager.h"
  88. #include "graphics toolbox.h"
  89. #include "graphics routines.h"
  90. #include "graphics libraries.h"
  91. #include "graphics debugging.h"
  92. #include "Getting Started GX - main.h"
  93.  
  94. #define    kOSEvent                        app4Evt    /* event used by MultiFinder */
  95. #define    kSuspendResumeMessage            1        /* high byte of suspend/resume event message */
  96. #define    kResumeMask                        1        /* bit of message field for resume vs. suspend */
  97.  
  98. Boolean            gQuitting;
  99.  
  100. gxStyle            gOurStyle;
  101. gxColor         gTextColor;
  102. gxTransform        gRotatedTransform;
  103. gxPoint            gTextLocation = {ff(75), ff(105)};
  104. long            gSleep = 0;
  105.  
  106. /*------ main -----------------------------------------------------------------------------------------*/
  107.  
  108. void main()
  109. {        
  110.     CursHandle            theCurs; 
  111.     Handle                menuBar;
  112.     gxGraphicsClient     client;
  113.     OSErr                err;
  114.     WindowPtr            wind;
  115.     
  116.     /**     
  117.         The GXNewGraphicsClient routine defines the graphics heap size. If you do not make this call, 
  118.         the GX graphics engine will create this heap automatically. How? It will create a heap which 
  119.         is a percentage of your application's ideal memory foot print. This call allows you to explicity 
  120.         define the ammount of memory used by the graphics system for it's graphics objects heap.
  121.     **/
  122.     client = GXNewGraphicsClient(nil, gGraphicsHeapSize * 1024, 0L);
  123.  
  124.     /**   Generic heap initialization.  **/
  125.     MaxApplZone(); 
  126.     MoreMasters(); MoreMasters(); MoreMasters(); 
  127.     MoreMasters(); MoreMasters(); MoreMasters(); 
  128.  
  129.     /** 
  130.         If gDebugging = TRUE, you will receive graphics library errors & notices will be posted.  This
  131.         functionality will only work with the "debugging" version of the QuickDraw GX init.  If this
  132.         init is not installed, these functions will not work. 
  133.     **/
  134.     if (gDebugging) {
  135.         SetGraphicsLibraryErrors ();
  136.         SetGraphicsLibraryNotices();    
  137.     }
  138.  
  139.     /** 
  140.         Set  "gGiveMeValidation" to TRUE, if you want run-time validation. As you increase the amount
  141.         of validation, The drawing speed will SLOW down due to all of the internal checking. 
  142.         
  143.         publicValidation will check parameters to public routines. For additional details regarding 
  144.         the various levels of validation, please see the documentation.
  145.     **/
  146.     if (gGiveMeValidation) GXSetValidation(gxPublicValidation); 
  147.  
  148.  
  149.     /** initialize the new graphics and printing environments.  **/
  150.  
  151.     GXEnterGraphics();
  152.     err = GXInitPrinting();
  153.  
  154.     /**   initialize the other managers, if no errors occured.   **/
  155.      
  156.      if (!err)
  157.      {
  158.          InitGraf(&qd.thePort);
  159.         InitFonts();
  160.         InitWindows();
  161.         InitMenus();
  162.         InitCursor();
  163.  
  164.         theCurs = GetCursor(watchCursor);
  165.         SetCursor(*theCurs);
  166.  
  167.         menuBar = GetNewMBar(rMenuBar); /* Create the menu bar */
  168.  
  169.         if (menuBar)
  170.         {
  171.             gQuitting = false;
  172.             SetMenuBar(menuBar);
  173.             DisposHandle(menuBar);
  174.             AddResMenu(GetMHandle(mApple), 'DRVR'); /* add Apple Menu items */
  175.             DrawMenuBar();
  176.  
  177.      // We initialize the CommonColors Library.  This will allow us to set the color of a
  178.      // shape by calling the SetShapeCommonColor function. We will need to call
  179.      // DisposeCommonColors when we leave, to clean up the world.
  180.  
  181.               InitCommonColors();
  182.     
  183.             DoCreateNew();
  184.             SetCursor(&qd.arrow);  
  185.             
  186.             while (!gQuitting)
  187.             {
  188.                 EventLoop();
  189.                 DoIdle(wind);
  190.             }
  191.  
  192.  
  193.     // Leaving.  Close all the windows so we get rid of any data we or GX created.  Then,
  194.     // dispose of the common colors and exit the GX printing and graphics environment.
  195.  
  196.             while (wind = FrontWindow())
  197.                 DoDispose(wind);
  198.  
  199.             DisposeCommonColors();
  200.             if (gOurStyle != nil) GXDisposeStyle (gOurStyle);
  201.             if (gRotatedTransform != nil) GXDisposeTransform (gRotatedTransform);
  202.         }
  203.  
  204.         GXExitPrinting();    /** Close the new printing mgr. **/
  205.     }
  206.     
  207.     GXExitGraphics();        /** Deallocate all of the default structures **/
  208.     GXDisposeGraphicsClient(client);
  209. }
  210.  
  211.  
  212. /*------ MyPrintingEventOverride -----------------------------------------------------------------------------*/
  213. // Override for GXPrintingEvent.  It allows us to update our windows
  214. // when the moveable modal printing dialogs are moved.  Keep this in your
  215. // main segment, which will always be loaded when GX tries to call the routine.
  216.  
  217. OSErr MyPrintingEventOverride(EventRecord *anEvent, Boolean filterEvent)
  218. {
  219.  
  220. // Handle events in whatever way is appropriate.  MyDoEvent is our
  221. // generic event handler.
  222.  
  223.     if (!filterEvent) MyDoEvent(anEvent);
  224.  
  225.     return noErr;
  226. }
  227.  
  228.  
  229.  
  230. /*------ EventLoop ------------------------------------------------------------------------------------*/
  231.  
  232. void EventLoop()
  233. {
  234.     EventRecord        theEvent;
  235.  
  236.     if (WaitNextEvent(everyEvent, &theEvent, gSleep, nil))
  237.         MyDoEvent(&theEvent);
  238. }
  239.  
  240.  
  241.  
  242. /*------ MyDoEvent ------------------------------------------------------------------------------------*/
  243.  
  244. void MyDoEvent(EventRecord *theEvent)
  245. {
  246.     char            key;
  247.     WindowPtr         whichWindow;
  248.     GrafPtr            oldPort;
  249.  
  250.     switch(theEvent->what)
  251.     {                    
  252.         case updateEvt:
  253.             if (((WindowPeek) theEvent->message)->windowKind == userKind)
  254.             {
  255.                 GetPort(&oldPort);
  256.                 SetPort((WindowPtr) theEvent->message);
  257.                 BeginUpdate((WindowPtr) theEvent->message);
  258.                 DoDraw((WindowPtr) theEvent->message);
  259.                 EndUpdate((WindowPtr) theEvent->message);
  260.                 SetPort(oldPort);
  261.               }
  262.         break;
  263.         
  264.         case mouseDown:
  265.         switch (FindWindow(theEvent->where, &whichWindow)) {
  266.             case inSysWindow:
  267.                 SystemClick(theEvent, whichWindow);
  268.             break;
  269.                     
  270.             case inDrag:
  271.                 if (((WindowPeek) whichWindow)->windowKind == userKind)
  272.                     DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
  273.             break;
  274.  
  275.             case inGoAway:
  276.                 if (((WindowPeek) whichWindow)->windowKind == userKind)
  277.                 {
  278.                     if (TrackGoAway(whichWindow, theEvent->where))
  279.                         DoDispose(whichWindow);
  280.                 }
  281.             break;
  282.             
  283.             case inContent:
  284.                 if ((whichWindow != FrontWindow()) &&
  285.                     (((WindowPeek) whichWindow)->windowKind == userKind))
  286.                      SelectWindow(whichWindow);
  287.             break;
  288.             case inMenuBar:
  289.                 DoMenuCommand(MenuSelect(theEvent->where));
  290.             break;
  291.  
  292.         }
  293.  
  294.         case keyDown:
  295.         case autoKey:
  296.             key = theEvent->message & charCodeMask;
  297.             if (theEvent->modifiers & cmdKey)
  298.                 if (theEvent->what == keyDown)
  299.                     DoMenuCommand(MenuKey(key));
  300.             break;
  301.  
  302.  
  303.         case kOSEvent:
  304.             switch ((unsigned long) theEvent->message >> 24) {    /**  high byte of message  **/
  305.                  case kSuspendResumeMessage:            /**  suspend/resume is also an activate/deactivate  **/
  306.                     if ((theEvent->message & kResumeMask) == 0)
  307.                        gSleep = 80;                    /** we are headed to the background, so slow down...  **/
  308.                     else
  309.                     {
  310.                        gSleep = 0;                    /** we are headed to the foreground, so speed up...  **/
  311.  
  312. // On a resume event, we need to call GXUpdateJob on all of our documents'
  313. // jobs.  This is important because the user may have just changed
  314. // something which affects our jobs (like the size of the paper in the
  315. // printer).
  316. //
  317. // Since our application stores our document references in the refCon fields
  318. // of our documents' windows, we just loop through every one of our windows,
  319. // extract our document pointers and update the associated jobs.
  320.  
  321.                         whichWindow = FrontWindow();
  322.                         
  323.                         while (whichWindow != nil)
  324.                         {
  325.                             if (((WindowPeek) whichWindow)->windowKind == userKind)
  326.                                 GXUpdateJob(GetDocJob(whichWindow));
  327.                             
  328.                             whichWindow = (WindowPtr) ((WindowPeek) whichWindow)->nextWindow;
  329.                         }
  330.                     }
  331.                 break;
  332.             }
  333.         break;
  334.     }
  335. }
  336.  
  337.  
  338.